<script src="../../store/index.js"></script>
<template>
    <div class="xgmm">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="旧的密码:" prop="oldpass">
                <el-input type="password" v-model="ruleForm.oldpass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="新的密码:" prop="newpass">
                <el-input type="password" v-model="ruleForm.newpass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码:" prop="checkPass">
                <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass1 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.newpass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                dat:[],
				id:localStorage.getItem('id'),
                ruleForm: {
                    oldpass:'',
                    newpass: '',
                    checkPass: '',
                },
                rules: {
                    oldpass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    newpass: [
                        { validator: validatePass1, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ]
                }
            };
        },
        created() {
            this.dat = this.$store.state.dat
        },
        methods: {
            submitForm(formName) {
			let that = this;
					if(that.ruleForm.newpass == that.ruleForm.checkPass &&
					 that.ruleForm.oldpass!="" &&
					  that.ruleForm.newpass!="" &&
					  that.ruleForm.checkPass!="") {
						this.$axios
							.post('http://localhost:8080/api/Expert/ResetExpertPassword', 
							this.$qs.stringify({ expertid: that.id,password:that.ruleForm.oldpass,newpassword:that.ruleForm.newpass }))
							.then(function(response) {
								// console.log(response.data);
								if(response.data){
									alert("修改成功");
									if(response.data){
									    that.$router.push({'path':'/index/bottom'})
									}
								}else {
									alert("修改失败");
								}
							})
							.catch(function(error) {
								console.log(error);
							});
					}
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style scoped="scoped">
    .xgmm{
        padding-top:50px ;
    }
    .el-form-item {
        width: 500px;
    }
</style>
